<?php
/**
 * Created by PhpStorm.
 * User: ytlmi
 * Date: 2017/11/8
 * Time: 9:23
 */
namespace app\sample_rsv\controller;

use think\Exception;
use app\common\controller\BaseController;
use app\common\commonClass\ExcelImportTool;
use app\common\commonClass\PageTools;
use app\sample_rsv\model\Sample;
use app\sample_rsv\model\Unit;
use app\sample_rsv\model\SampleType as Type;

class Group extends BaseController
{
	protected $model='app\sample_rsv\model\Group';
    protected $partName='委托管理';

    /*
     * 查看委托详情
     */
    public function detail(int $id)
    {
        $this->assign('nav', PageTools::nav((config('nav_items'))));
        $this->assign('id',$id);
        return $this->fetch();
    }

    /**
     * @throws \Exception
     */
    //导入
    public function import()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            //保存文件
            $savePath=ROOT_PATH . 'public' . DS . 'uploads'.DS.'xls';
            $info = $file->move($savePath);
            $filePath=$savePath.DS.$info->getSaveName();

            //格式化保存数据并返回
            $arr_header=config('header_group');
            $arr_require_once=config('require_once_group');//首行非空
            $arr_require_always=config('require_always_group');//全程非空
            $construct=config('construct');
            $result=ExcelImportTool::instance($filePath)->header($arr_header,$construct)->check($arr_require_always,$arr_require_once)->toArray();//格式化数据

            foreach ($result as $item) {
                //存储委托信息
                $group['name']=$item['group_name'];
                $group['id_unit']=Unit::where('name',$item['unit'])->value('id');
                if(!$group['id_unit']) return('单位名称不存在：'.$item['unit']);
                $group['agent_name']=$item['agent_name'];
                if($this->M->where('name',$group['name'])->select()){
                    return '委托名已存在';
                }else{
                    $this->M->data($group, true)->allowField(true)->save();
                    $id_group=$this->M->id;
                }

                foreach ($item['inner'] as $inner) {
                    //存储样品信息
                    $arr_sex=['男'=>'0','女'=>'1'];
                    $sample['sample_name']=$inner['sample']['sample_name'];
                    $sample['id_type']=Type::where('name',$inner['sample']['sample_type'])->value('id');
                    $sample['person_name']=$inner['sample']['person_name'];
                    in_array($inner['sample']['person_sex'],['男','女']) ? $sample['person_sex']=$arr_sex[$inner['sample']['person_sex']] :$sample['person_sex']='';
                    $sample['person_race']=$inner['sample']['person_race'];
                    if(!$sample['id_type']) return('样本类别不存在：'.$inner['sample']['sample_type']);
                    $sample['id_group']=$id_group;
                    $sampleM = new Sample;
                    $sampleM->data($sample, true)->allowField(true)->save();
                }
            }
            return count($result);
        }else{
            return false;
        }
    }


}